package com.argrace.smart.ui;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.util.AlinkHttpUtil;
import com.zhouyou.view.seekbar.SignSeekBar;

import java.util.HashMap;
import java.util.Locale;

/**
 * Created by argrace_dev_1 on 2018/3/9.
 */

public class AirCondition_RelationView extends LinearLayout {
    private Context context;
    private int position = 0;
    DrawableSwitch drawableSwitch;
    TextView currentTemperature;
    TextView tagTemperature;
    ImageView tagtemperature_up;
    ImageView tagtemperature_down;
    SignSeekBar fengsu;
    LinearLayout model_code_ll, model_hot_ll, model_wind_ll, model_water_ll;
    private String uuid;
    private String mode = "";
    private boolean is_sendMsg = false;

    public LinearLayout getModel_code_ll() {
        return model_code_ll;
    }

    public LinearLayout getModel_hot_ll() {
        return model_hot_ll;
    }

    public LinearLayout getModel_wind_ll() {
        return model_wind_ll;
    }

    public LinearLayout getModel_water_ll() {
        return model_water_ll;
    }

    public DrawableSwitch getDrawableSwitch() {
        return drawableSwitch;
    }

    public TextView getCurrentTemperature() {
        return currentTemperature;
    }

    public TextView getTagTemperature() {
        return tagTemperature;
    }

    public ImageView getTagtemperature_up() {
        return tagtemperature_up;
    }

    public ImageView getTagtemperature_down() {
        return tagtemperature_down;
    }

    public SignSeekBar getFengsu() {
        return fengsu;
    }

    public AirCondition_RelationView(Context context, String uuid, boolean is_sendMsg) {
        super(context);
        this.context = context;
        this.uuid = uuid;
        this.is_sendMsg = is_sendMsg;
        init();
    }

    public AirCondition_RelationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public AirCondition_RelationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public String getRelaitionJson() {
        StringBuffer sb = new StringBuffer();
        if (getDrawableSwitch().isSwitchOn()) {
            sb.append("\"Switch_" + position  + "\""+ ":");
            Key_ValueEntity entity = new Key_ValueEntity();
            entity.setValue("1");
            sb.append(entity.toString());
            sb.append(",");
        } else {
            sb.append("\"Switch_" + position  + "\""+ ":");
            Key_ValueEntity entity = new Key_ValueEntity();
            entity.setValue("0");
            sb.append(entity.toString());
            sb.append(",");
        }
        sb.append("\"WindSpeed_" + position  + "\""+ ":");
        Key_ValueEntity entity_windspeed = new Key_ValueEntity();
        //sds控制台数值为2、3、4对应progress0、1、2
        entity_windspeed.setValue(String.valueOf(fengsu.getProgress() + 2));
        sb.append(entity_windspeed.toString());
        sb.append(",");

        sb.append("\"CurrentTemperature_" + position + "\"" + ":");
        Key_ValueEntity entity_currenttemperature = new Key_ValueEntity();
        entity_currenttemperature.setValue(currentTemperature.getText().toString().substring(0, 2));
        sb.append(entity_currenttemperature.toString());
        sb.append(",");


        sb.append("\"WorkMode_" + position + "\"" + ":");
        Key_ValueEntity entity_workmode = new Key_ValueEntity();
        entity_workmode.setValue(mode);
        sb.append(entity_workmode.toString());
        sb.append(",");
        return sb.toString();
    }

    private void init() {
        LayoutInflater.from(context).inflate(
                R.layout.item_relation_aircondition, this, true);
        drawableSwitch = (DrawableSwitch) findViewById(R.id.item_aircondition_switch);
        currentTemperature = (TextView) findViewById(R.id.item_aircondition_currenttemperature);
        tagTemperature = (TextView) findViewById(R.id.item_aircondition_tagtemperature);
        tagtemperature_up = (ImageView) findViewById(R.id.item_aircondition_tagtemperature_up);
        tagtemperature_down = (ImageView) findViewById(R.id.item_aircondition_tagtemperature_down);
        fengsu = (SignSeekBar) findViewById(R.id.item_aircondition_fengsu);
        model_code_ll = (LinearLayout) findViewById(R.id.item_aircondition_model_code_ll);
        model_hot_ll = (LinearLayout) findViewById(R.id.item_aircondition_model_hot_ll);
        model_wind_ll = (LinearLayout) findViewById(R.id.item_aircondition_model_wind_ll);
        model_water_ll = (LinearLayout) findViewById(R.id.item_aircondition_model_water_ll);
        fengsu.getConfigBuilder()
                .min(0)
                .max(2)
                .progress(1)
                .sectionCount(2)
                //.trackColor(ContextCompat.getColor(getContext(), R.color.color_gray))
                //.secondTrackColor(ContextCompat.getColor(getContext(), R.color.color_blue))
                .thumbColor(ContextCompat.getColor(context, R.color.elune_blue))
                .sectionTextColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .sectionTextSize(16)
                //.thumbTextColor(ContextCompat.getColor(getContext(), R.color.color_red))
                //.thumbTextSize(18)
                //.signColor(ContextCompat.getColor(getContext(), R.color.color_green))
                //.signTextSize(18)
                .sectionTextPosition(SignSeekBar.TextPosition.BELOW_SECTION_MARK)
                .build();
        drawableSwitch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int state = drawableSwitch.isSwitchOn() ? 0 : 1;
                drawableSwitch.setSwitchOn(!drawableSwitch.isSwitchOn());
                drawableSwitch.invalidate();
            }
        });
        tagtemperature_up.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int current_tagtemperature = Integer.parseInt(currentTemperature.getText().toString().substring(0, 2));
                current_tagtemperature++;
                if(current_tagtemperature>= 32)
                {
                    current_tagtemperature = 32;
                }
                currentTemperature.setText(String.valueOf(current_tagtemperature) + "℃");

            }
        });
        tagtemperature_down.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int current_tagtemperature = Integer.parseInt(currentTemperature.getText().toString().substring(0, 2));
                current_tagtemperature--;
                if(current_tagtemperature <= 16)
                {
                    current_tagtemperature = 16;
                }
                currentTemperature.setText(String.valueOf(current_tagtemperature) + "℃");
            }
        });
        model_code_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //可用model替换参数
                mode = "1";
                setModel("1");
            }
        });
        model_hot_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mode = "2";
                setModel("2");
            }
        });
        model_wind_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mode = "3";
                setModel("3");
            }
        });
        model_water_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mode = "4";
                setModel("4");
            }
        });
    }


    public void setPosition(int position) {
        this.position = position;
    }

    private AirControlListeners listeners;

    private void setListeners(AirControlListeners l) {
        this.listeners = l;
    }

    private interface AirControlListeners {
        void onAirClick();
    }

    public void setModel(String model) {
        switch (model) {
            case "1":
                model_code_ll.setBackgroundColor(Color.YELLOW);
                model_hot_ll.setBackgroundColor(Color.WHITE);
                model_wind_ll.setBackgroundColor(Color.WHITE);
                model_water_ll.setBackgroundColor(Color.WHITE);
                break;
            case "2":
                model_code_ll.setBackgroundColor(Color.WHITE);
                model_hot_ll.setBackgroundColor(Color.YELLOW);
                model_wind_ll.setBackgroundColor(Color.WHITE);
                model_water_ll.setBackgroundColor(Color.WHITE);
                break;
            case "3":
                model_code_ll.setBackgroundColor(Color.WHITE);
                model_hot_ll.setBackgroundColor(Color.WHITE);
                model_wind_ll.setBackgroundColor(Color.YELLOW);
                model_water_ll.setBackgroundColor(Color.WHITE);
                break;
            case "4":
                model_code_ll.setBackgroundColor(Color.WHITE);
                model_hot_ll.setBackgroundColor(Color.WHITE);
                model_wind_ll.setBackgroundColor(Color.WHITE);
                model_water_ll.setBackgroundColor(Color.YELLOW);
                break;
        }
    }

    public void setWind(String wind) {
        fengsu.setProgress(Long.parseLong(wind) - 2);
    }
}
